package com.binvshe.binvshe.common.http;

import android.support.annotation.NonNull;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.pachong.android.framework.httprequest.HttpRequest;
import com.pachong.android.framework.httprequest.IResponseDataParser;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by VeiVi on 2015/9/23.
 * <p>
 * 解析规则，每个app或许有自己不同的解析方法
 */
public class MyResponseParser<BeanT extends Object> implements IResponseDataParser {
    private JSONObject mJsonObject;
    private Class<BeanT> mClazz;

    public MyResponseParser(@NonNull Class<BeanT> clazz) {
        mClazz = clazz;
    }

    public void setJsonString(String jsonStr) {
        try {
            mJsonObject = new JSONObject(jsonStr);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public String getJsonString() {
        return mJsonObject.toString();
    }

    /**
     * mJsonObject 必须不为空， 这个在 isMyRules 会做判断
     *
     * @return
     */
    public boolean isSuccessful() {
        return true;
    }


    /**
     * mJsonObject 必须不为空， 这个在 isMyRules 会做判断
     *
     * @return
     */
    public String getMessage() {
        return mJsonObject.optString("msg");
    }

    /**
     * mJsonObject 必须不为空， 这个在 isMyRules 会做判断
     *
     * @return
     */
    public int getCode() {
        return mJsonObject.optInt("result");
    }


    /**
     * mJsonObject 必须不为空， 这个在 isMyRules 会做判断
     *
     * @return
     */
    public boolean hasMore() {
        JSONObject jobj = getDataJsonObj();
        if (jobj != null) {
            int pageNo = jobj.optInt("page_no"); // default 0
            int totalPages = jobj.optInt("total_pages"); // default 0
            return pageNo < totalPages? true : false;
        }
        return true;

    }


    public Object getData() {
        if (mJsonObject != null && !TextUtils.isEmpty(mJsonObject.toString().trim())) {
            JSONArray array = mJsonObject.optJSONArray("items");
//            int hasMoreInt = dataObj.optInt("has_more", -1);
            // 先解析 JSONArray ， 如果data不是数组的形式再判断对象的形式
            if (array != null) {
//            Log.e("vivi", "parse data array");
                if (array.length() > 0) {
                    List<BeanT> beanList = new ArrayList<BeanT>();
                    for (int i = 0; i < array.length(); i++) {
                        Gson gson = new Gson();
                        BeanT bean = gson.fromJson(array.opt(i).toString(), mClazz);
                        beanList.add(bean);
                    }
                    return beanList;
                } else {
                    return null;
                }
            } else {
                // data不是JSONArray，则判断是否是 JSONObject
                HttpRequest.log("parse json string == " + mJsonObject.toString());
                BeanT bean = null;
                if (mClazz == String.class) {
                    HttpRequest.log("(bean instanceof String ");
                    return mJsonObject.toString();
                }
                try {
                    Gson gson = new Gson();
                    bean = gson.fromJson(mJsonObject.toString(), mClazz);

                } catch (Exception e) {
                    e.printStackTrace();
                    HttpRequest.log("ResponseParser getData 解析错误");
                    bean = null;
                }
                return bean;
            }
        }

        return null;
    }

    public boolean isMyRules() {
        return true;
    }

    @Override
    public JSONObject getDataJsonObj() {
        return mJsonObject;

    }

}
